# SharePoint PowerShell Module
# http://powershell.codeplex.com

Function Get-SPFarm {
    $SPFarm = [Microsoft.SharePoint.Administration.SPFarm]::Local
    return $SPFarm
}

Function Get-SPServer {
    Param (
        [ValidateNotNullOrEmpty()]
        [string] $Identity
    )

    if ($Identity) {
        return [Microsoft.SharePoint.Administration.SPFarm]::Local.Servers | Where-Object {$_.Address -eq "$Identity"}
    }
    else {
        return [Microsoft.SharePoint.Administration.SPFarm]::Local.Servers
    }
}

Function Get-SPServiceApplication {
    Param (
        [ValidateNotNullOrEmpty()]
        [string] $Identity
    )

    if ($Identity) {
        return [Microsoft.SharePoint.Administration.SPFarm]::Local.Services | Where-Object {$_.Name -eq "$Identity"}
    }
    else {
        return [Microsoft.SharePoint.Administration.SPFarm]::Local.Services
    }
}

Function Get-SPSearchService {
    return Get-SPServiceApplication | Where-Object {$_.TypeName -match "SharePoint Foundation Search|Windows SharePoint Services Help Search"}
}

Function Get-SPEnterpriseSearchService {
    return Get-SPServiceApplication | Where-Object {$_.TypeName -match "SharePoint Server Search|Office SharePoint Server Search"}
}

Function Get-SPWebApplication {
    Param (
        [ValidateNotNullOrEmpty()]
        [string] $Identity
    )

    if ($Identity) {
        return [Microsoft.SharePoint.Administration.SPFarm]::Local.Services | Where-Object {$_.GetType().FullName -eq "Microsoft.SharePoint.Administration.SPWebService"} | ForEach-Object {$_.WebApplications | Where-Object {$_.Name -eq "$Identity"}}
    }
    else {
        return [Microsoft.SharePoint.Administration.SPFarm]::Local.Services | Where-Object {$_.GetType().FullName -eq "Microsoft.SharePoint.Administration.SPWebService"} | ForEach-Object {$_.WebApplications}
    }
}

Function Get-SPSite {
    Param (
        [ValidateNotNullOrEmpty()]
        [string] $Identity
    )

    if ($Identity) {
        $SPSite = New-Object Microsoft.SharePoint.SPSite("$Identity")
    }
    else {
        $SPSite = Get-SPWebApplication | ForEach-Object {$_.Sites}
    }

    foreach ($siteColl in $SPSite) {
        $siteColl
        $siteColl.Dispose()
    }
}

Function Get-SPContentDatabase {
    Param (
        [ValidateNotNullOrEmpty()]
        [string] $Identity,
        [string] $WebApplication,
        [string] $Site
    )

    $tempArray = @()
    $SPContentDbIdMethod = [Microsoft.SharePoint.Administration.SPContentDatabase].GetMethod("get_ID")
    $SPContentDbNameMethod = [Microsoft.SharePoint.Administration.SPContentDatabase].GetMethod("get_Name")
    $SPContentDbWebApplicationMethod = [Microsoft.SharePoint.Administration.SPContentDatabase].GetMethod("get_WebApplication")
    $SPContentDbServerMethod = [Microsoft.SharePoint.Administration.SPContentDatabase].GetMethod("get_Server")
    $SPContentDbCurrentSiteCountMethod = [Microsoft.SharePoint.Administration.SPContentDatabase].GetMethod("get_CurrentSiteCount")
    $SPContentDbDiskSizeRequiredMethod = [Microsoft.SharePoint.Administration.SPContentDatabase].GetMethod("get_DiskSizeRequired")
    $SPContentDbStatusMethod = [Microsoft.SharePoint.Administration.SPContentDatabase].GetMethod("get_Status")

    if ($WebApplication) {
        $SPsiteCollections = Get-SPWebApplication -Identity "$WebApplication" | ForEach-Object {$_.Sites}
    }
    else {
        $SPsiteCollections = Get-SPWebApplication | ForEach-Object {$_.Sites}
    }

    if ($Site) {
        $SPSite = $SPsiteCollections | Where-Object {$_.Url -eq "$Site"}
    }
    else {
        $SPSite = $SPsiteCollections
    }

    foreach ($siteColl in $SPSite) {
        $SPContentDbId = $SPContentDbIdMethod.Invoke($siteColl.ContentDatabase, "instance,public", $null, $null, $null)
        $SPContentDbName = $SPContentDbNameMethod.Invoke($siteColl.ContentDatabase, "instance,public", $null, $null, $null)
        $SPContentDbWebApplication = $SPContentDbWebApplicationMethod.Invoke($siteColl.ContentDatabase, "instance,public", $null, $null, $null)
        $SPContentDbServer = $SPContentDbServerMethod.Invoke($siteColl.ContentDatabase, "instance,public", $null, $null, $null)
        $SPContentDbCurrentSiteCount = $SPContentDbCurrentSiteCountMethod.Invoke($siteColl.ContentDatabase, "instance,public", $null, $null, $null)
        $SPContentDbDiskSizeRequired = $SPContentDbDiskSizeRequiredMethod.Invoke($siteColl.ContentDatabase, "instance,public", $null, $null, $null)
        $SPContentDbStatus = $SPContentDbStatusMethod.Invoke($siteColl.ContentDatabase, "instance,public", $null, $null, $null)

        if ($Identity) {
            if ($SPContentDbName -eq "$Identity") {
                $SPContentDatabase = New-Object System.Object
                $SPContentDatabase | Add-Member -Type NoteProperty -Name Id -Value $SPContentDbId
                $SPContentDatabase | Add-Member -Type NoteProperty -Name Name -Value $SPContentDbName
                $SPContentDatabase | Add-Member -Type NoteProperty -Name WebApplication -Value $SPContentDbWebApplication
                $SPContentDatabase | Add-Member -Type NoteProperty -Name Server -Value $SPContentDbServer
                $SPContentDatabase | Add-Member -Type NoteProperty -Name CurrentSiteCount -Value $SPContentDbCurrentSiteCount
                $SPContentDatabase | Add-Member -Type NoteProperty -Name DiskSizeRequired -Value $SPContentDbDiskSizeRequired
                $SPContentDatabase | Add-Member -Type NoteProperty -Name Status -Value $SPContentDbStatus
                if (-not($tempArray |Where-Object {$_.Id -eq $SPContentDbId})) {
                    $tempArray += $SPContentDatabase
                }
            }
        }
        else {
            $SPContentDatabase = New-Object System.Object
            $SPContentDatabase | Add-Member -Type NoteProperty -Name Id -Value $SPContentDbId
            $SPContentDatabase | Add-Member -Type NoteProperty -Name Name -Value $SPContentDbName
            $SPContentDatabase | Add-Member -Type NoteProperty -Name WebApplication -Value $SPContentDbWebApplication
            $SPContentDatabase | Add-Member -Type NoteProperty -Name Server -Value $SPContentDbServer
            $SPContentDatabase | Add-Member -Type NoteProperty -Name CurrentSiteCount -Value $SPContentDbCurrentSiteCount
            $SPContentDatabase | Add-Member -Type NoteProperty -Name DiskSizeRequired -Value $SPContentDbDiskSizeRequired
            $SPContentDatabase | Add-Member -Type NoteProperty -Name Status -Value $SPContentDbStatus
            if (-not($tempArray |Where-Object {$_.Id -eq $SPContentDbId})) {
                $tempArray += $SPContentDatabase
            }
        }
    }
    return $tempArray
}

Function Get-SPWeb {
    Param (
        [ValidateNotNullOrEmpty()]
        [Parameter(Mandatory=$true)]
        [string] $Identity
    )

    $OldErrorActionPreference = $ErrorActionPreference
    $ErrorActionPreference = "SilentlyContinue"
    foreach ($siteColl in Get-SPSite) {
        foreach ($subWeb in $siteColl.AllWebs) {
            if ($subWeb.Url -eq "$Identity") {
                $subWeb
                $subWeb.Dispose()
            }
        }
        $siteColl.Dispose()
    }
    $ErrorActionPreference = $OldErrorActionPreference
}

Function Get-SPSolution {
    Param (
        [ValidateNotNullOrEmpty()]
        [string] $Identity
    )

    if ($Identity) {
        return [Microsoft.SharePoint.Administration.SPFarm]::Local.Solutions | Where-Object {$_.Name -eq "$Identity"}
    }
    else {
        return [Microsoft.SharePoint.Administration.SPFarm]::Local.Solutions
    }
}

Function Get-SPFeature {
    Param (
        [ValidateNotNullOrEmpty()]
        [string] $Identity,
        [string] $WebApplication,
        [string] $Site,
        [string] $Web
    )

    if ($WebApplication) {
        $SPFeature = Get-SPWebApplication -Identity $WebApplication | ForEach-Object {$_.Features | ForEach-Object {$_.Definition}}
    }
    elseif ($Site) {
        $SPFeature = foreach ($siteColl in Get-SPSite -Identity $Site) {
            $siteColl.Features | ForEach-Object {$_.Definition}
            $siteColl.Dispose()
        }
    }
    elseif ($Web) {
        $SPFeature = foreach ($subWeb in Get-SPWeb -Identity $Web) {
            $subWeb.Features | ForEach-Object {$_.Definition}
            $subWeb.Dispose()
        }
    }
    else {
        $SPFeature = [Microsoft.SharePoint.Administration.SPFarm]::Local.FeatureDefinitions
    }

    if ($Identity) {
        return $SPFeature | Where-Object {$_.DisplayName -eq "$Identity"}
    }
    else {
        return $SPFeature
    }
}